import defaultImg from '@/components/defaultImg/defaultImg'
import { AddMobileContactData } from '@/utils/common-api.js'
export default {
  data() {
    return {
      titleList: [
        {
          name: '已报价',
        },
        {
          name: '待确认合同',
        },
        {
          name: '已过期',
        },
        {
          name: '沟通',
        },
        {
          name: '我的订单',
        },
      ],
      current: 0, // tabs组件的current值，表示当前活动的tab选项
      swiperCurrent: 0, // swiper组件的current值，表示当前那个swiper-item是活动的
      show: false,
      options: [
        {
          text: '删除',
          style: {
            backgroundColor: '#dd524d',
          },
        },
      ],
      btnWidth: 138,
      hushow: true,

      params: {
        pageIndex: 1,
        pageSize: 10,
        type: 0, //默认 0已报价，1待确认合同 ，2 以过期
        store_Id: '',
      },
      Myofferlist: [],
      chatList: [], //所有联系人列表
      IMlist: [], //报价联系人列表
    }
  },
  components: {
    defaultImg,
  },
  onLoad() {
    console.log(this.$store.state)
    this.params.store_Id = this.$store.state.userInfoDetail.store.storeId
    this.getlist(0)
    // 获取所有联系人列表
    this.getChatList()
  },
  methods: {
    // 获取报价列表
    async getlist(type) {
      this.params.type = type
      try {
        const res = await this.$api.findCar.GetCarFindBMyOfferList(this.params)

        res.data.list.forEach((it) => {
          it.facade_PictureList.forEach((item) => {
            if (item.inColour) {
              item.inColour.neiColor = item.inColour.colorCoding
                ? item.inColour.colorCoding.split('/')
                : []
            }
            if (item.outerColour) {
              item.outerColour.neiColor = item.outerColour.colorCoding
                ? item.outerColour.colorCoding.split('/')
                : []
            }
          })
        })

        this.Myofferlist = this.Myofferlist.concat(res.data.list)
        console.log(this.Myofferlist)
      } catch (e) {
        //TODO handle the exception
      }
    },
    open(index) {
      this.listts[index].show = true
      this.listts.map((val, idx) => {
        if (index !== idx) this.listts[idx].show = false
      })
    },
    // 返回报价时间
    returnTime(val) {
      if (val) {
        return (
          val.split('T')[0].split('-')[1] +
          ' ' +
          val.split('T')[0].split('-')[2] +
          ' ' +
          val.split('T')[1]
        )
      }
    },
    // tabs通知swiper切换
    tabsChange(index) {
      this.swiperCurrent = index
      console.log(index)
      this.Myofferlist = []
      if (index < 3) {
        this.getlist(index)
      } else if (index === 3) {
        this.getTimList()
      } else if (index === 4) {
        uni.navigateTo({
          url: '/pages_order/orderAdmin/orderAdmin',
        })
      }
    },
    // swiper-item左右移动，通知tabs的滑块跟随移动
    transition(e) {
      let dx = e.detail.dx
      this.$refs.uTabs.setDx(dx)
    },
    // 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
    // swiper滑动结束，分别设置tabs和swiper的状态
    animationfinish(e) {
      let current = e.detail.current
      this.$refs.uTabs.setFinishCurrent(current)
      this.swiperCurrent = current
      this.current = current
    },
    // scroll-view到底部加载更多
    onreachBottom() {
      console.log('没有')
    },

    //联系买家
    contactSeller(data) {
      console.log(data)
      AddMobileContactData(this.$store.state.userInfoDetail.store.storeId)
      // 传和我聊天人的信息
      this.$store.commit('news/createConversationActive', {
        user: data.store_Name, //店铺名称
        img: data.store_Topimg, //店铺头像
        lastTime: '', //
        userId: data.store_Id, //店铺id
        conversationID: 'C2C' + data.store_Id,
      })
      this.$store.commit('news/setCardInfo', {
        imgSrc: data.cartypeUrl,
        name: data.car_Type_Name + ' ' + data.car_Name,
        price: data.quoted_Price,
        labelPrice: data.price,
        type: 'findCar',
      })
      uni.navigateTo({
        url:
          '/pages_news/contactSeller/contactSeller?user=' +
          data.store_Name +
          '&pageId=' +
          data.car_find_id +
          '&tab=0',
      })
    },
    //打电话
    phoneClick(phone) {
      AddMobileContactData(this.$store.state.userInfoDetail.store.storeId)
      uni.makePhoneCall({
        phoneNumber: phone, //仅为示例
      })
    },
    // 获取所有沟通列表
    async getChatList() {
      try {
        let data = {
          pageIndex: 1,
          pageSize: 10,
          channel: 0,
          module: 30,
          active_Store_Id: this.$store.state.userInfoDetail.store.storeId,
          active_User_Id: this.$store.state.tid,
          passive_Store_Id: '',
          passive_User_Id: '',
        }
        const res = await this.$api.chat.GetChatFlowList(data)
        this.chatList = res.data
      } catch (e) {
        //TODO handle the exception
      }
    },
    getTimList() {
      let promise = this.tim.getConversationList()
      promise
        .then((res) => {
          let conversationList = res.data.conversationList // 会话列表，用该列表覆盖原有的会话列表
          console.log(this.chatList, conversationList)
          if (conversationList.length) {
            //将返回的会话列表拼接上 用户的基本资料
            //说明：如果已经将用户信息 提交到tim服务端了 就不需要再次拼接
            this.$store.commit('news/updateConversationList', conversationList)
            this.IMlist = []
            for (let i = 0; i < conversationList.length; i++) {
              for (let j = 0; j < this.chatList.length; j++) {
                if (
                  'C2C' + this.chatList[j].passive_Store_Id ===
                  conversationList[i].conversationID
                ) {
                  this.IMlist.push({
                    user: conversationList[i].userProfile.nick,
                    img: conversationList[i].userProfile.avatar,
                    userId: conversationList[i].userProfile.userID,
                    lastTime: conversationList[i].lastMessage.lastTime,
                    unreadCount: conversationList[i].unreadCount,
                    conversationID: conversationList[i].conversationID,
                    messageForShow:
                      conversationList[i].lastMessage.messageForShow,
                  })
                  break
                }
              }
            }
            console.log('联系人列表', this.IMlist)
            if (this.IMlist.lenght === 0) {
              this.showDefault = true
            }
          } else {
            this.showDefault = true
          }
        })
        .catch((err) => {
          console.warn('getConversationList error:', err) // 获取会话列表失败的相关信息
        })
    },
    // 沟通列表点进去聊天
    contentClick(i) {
      console.log(i)
      this.$store.commit('news/createConversationActive', this.IMlist[i])
      uni.navigateTo({
        url: '/pages_news/room/room?user=' + this.IMlist[i].user,
      })
    },
    //报价详情
    baojiaclick(item) {
      if (item.state === 1) {
        this.ConfirmContract(item.contractId)
      } else {
        uni.navigateTo({
          url:
            '/pages_findCar/detailsOfMyOffer/detailsOfMyOffer?id=' +
            item.car_find_id,
        })
      }
    },
    ConfirmContract(id) {
      uni.navigateTo({
        url: '/pages_order/carOrderDetail/carOrderDetail?id=' + id + '&tab=1',
      })
    },
  },
}
